{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2c41ceed",
   "metadata": {},
   "source": [
    "--------\n",
    "* 记录人：纪冰儿\n",
    "* 记录时间：2021-11-22\n",
    "* week13\n",
    "* 主要参考文档；\n",
    "> 1.[python官方文档](http://python.org/doc)\n",
    "> 2.[博客_函数]\n",
    "\n",
    "--------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7adc6bd",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "> 1.函数的定义\n",
    "> 2.如何使用函数\n",
    "> 3.函数的返回值\n",
    "> 4.使用注释改进文档\n",
    "> 5.传递任意数量的实参\n",
    "> 6.函数与模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd4af844",
   "metadata": {},
   "source": [
    "## 1.函数的定义\n",
    "\n",
    "* 定义函数：\n",
    "> 1.写def: def 定义函数的第一步\n",
    "> 2.写定义函数的名称：def function_name\n",
    ">> 1.function_name 遵循变量名称定义的规则\n",
    "> 3.函数名称后加上“（）：”def function_name():  ===> f(x) = 2x\n",
    "\n",
    "* 书写函数体\n",
    "> 1.函数的注释\n",
    "> 2.写函数的具体操作（函数体）\n",
    "> 3.注意：函数也是需要有tab键的间距的\n",
    "\n",
    "* 使用函数(函数的调用)\n",
    "> 1.function_name()\n",
    "\n",
    "* 函数的意义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "82447b0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):    # write Fibonacci series up to n\n",
    "    \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "    a,b = 0,1\n",
    "    while a < n:\n",
    "        print(a, end=' ')\n",
    "        a,b = b,a+b\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "90800e6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27559d36",
   "metadata": {},
   "source": [
    "## 尝试写一个实现hello word 的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fb983aa3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello():\n",
    "    '''打印hello world'''\n",
    "    print('hello world!')\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "78b838e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "73173de6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function fib in module __main__:\n",
      "\n",
      "fib(n)\n",
      "    Print a Fibonacci series up to n.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(fib)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8aa01ca3",
   "metadata": {},
   "source": [
    "# 2.如何使用函数（课本149）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7773d860",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 思考函数存在的意义：\n",
    "# 1.可以通过调用函数的方式实现某个功能，使用人不需要考虑函数代码内容\n",
    "# 2.节省时间，提升应用的效率\n",
    "# 3.一旦定义，就可按照一定的规则重复使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "8343ca9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义了一个名字叫“search4vowels”函数\n",
    "def search4vowels(word):\n",
    "    \"\"\"\n",
    "    打印文本中出现的元音字母\n",
    "    请输入你想要查询的文本内容\n",
    "    \"\"\"\n",
    "    vowels = set('aeiou')\n",
    "      #  word = input(\"Provide a word to search for vowels:\")\n",
    "    found = vowels.intersection(set(word)) # 交集运算\n",
    "    print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "007e8357",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search4vowels in module __main__:\n",
      "\n",
      "search4vowels(word)\n",
      "    打印文本中出现的元音字母\n",
      "    请输入你想要查询的文本内容\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search4vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "9d15e7f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'i'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"kitty\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "271abc26",
   "metadata": {},
   "outputs": [],
   "source": [
    "def best_friend():\n",
    "    \"\"\"打印文希，婉婷，杨汇，树林，林琪\"\"\"\n",
    "    print(\"文希，婉婷，杨汇，树林，林琪\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4724a4b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文希，婉婷，杨汇，树林，林琪\n"
     ]
    }
   ],
   "source": [
    "best_friend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ba421069",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function best_friend in module __main__:\n",
      "\n",
      "best_friend()\n",
      "    打印文希，婉婷，杨汇，树林，林琪\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(best_friend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f6fb69e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面的方法太重复了，主要原因是没有对重复功能的代码块进行特殊的定义如（名称、注释）等"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5a8be58",
   "metadata": {},
   "source": [
    "# 函数的参数\n",
    "\n",
    "* <font style = ‘color:red’> 注意：函数的参数可以没有，如果有参数，则如下（分为形式参数和实际参数：）</font>\n",
    "\n",
    "* > 1.形式参数(形参)\n",
    ">> 1.在定义过程中。函数的参数为形式参数\n",
    ">> 2.形式参数是虚拟的，不占用空间的\n",
    "* > 2.实际参数(实参）\n",
    ">> 1.调用函数过程中，函数的参数为实际参数\n",
    ">> 2.调用函数时，形式参数被分配空间，成为实际参数并应用起来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "39df8dc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(username):    \n",
    "    \"\"\"显示简单的问候语\"\"\"    \n",
    "    print(\"hello,\"+username.title()+\"!\")         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "479148de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,Jibinger!\n"
     ]
    }
   ],
   "source": [
    "greet_user('jibinger')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "9042f8d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,Jibinger!\n"
     ]
    }
   ],
   "source": [
    "# 调用函数：username = ‘jibinger’\n",
    "greet_user('jibinger')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5de55d5",
   "metadata": {},
   "source": [
    "* 当参数不止一个时：\n",
    "> 1.位置参数\n",
    ">> 1.形式参数有多个值，如果实际参数直接赋值，要按照形式参数的先后顺序，这种方式称：位置参数赋值\n",
    ">> 2. 在调用函数过程中需要考虑如下:\n",
    ">>> 2.考虑位置\n",
    ">>> 3.考虑个数\n",
    "> 2.关键字参数\n",
    "> 3. 默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6f9a1b96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a hamster.\n",
      "My hamster's name is Harry.\n",
      "\n",
      "I have a cat.\n",
      "My cat's name is Python.\n"
     ]
    }
   ],
   "source": [
    "#zhichao\n",
    "\n",
    "def describe_pet(animal_type,pet_name):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n",
    "# 运行\n",
    "describe_pet('hamster','harry')\n",
    "describe_pet('cat','python')  #函数调用多次是一种效率极高的工作方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "734baac9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a hamster.\n",
      "My hamster's name is Cat.\n"
     ]
    }
   ],
   "source": [
    "# 缺少一个需要被定义的位置参数\n",
    "describe_pet('hamster','cat')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "46c1f9a2",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "describe_pet() missing 1 required positional argument: 'pet_name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-f6a386e95c13>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdescribe_pet\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'dog'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: describe_pet() missing 1 required positional argument: 'pet_name'"
     ]
    }
   ],
   "source": [
    "describe_pet('dog')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "67a7d6a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a cat.\n",
      "My cat's name is Hamster.\n"
     ]
    }
   ],
   "source": [
    "# 关键字参数\n",
    "describe_pet(animal_type='cat',pet_name='hamster')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d4da1670",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 默认参数（写在形式参数中的）\n",
    "def describe_pet(pet_name,animal_type=\"dog\"):\n",
    "    \"\"\"显示宠物信息，animal_type 的默认值 'dog'\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fec58085",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is Cookie.\n"
     ]
    }
   ],
   "source": [
    "describe_pet('cookie','dog')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6e04956",
   "metadata": {},
   "source": [
    "* 实操和注意（参数部分）positional argument follows keyword argument\n",
    "* > 1.如果想关键字参数和位置参数共同使用\n",
    ">> 1.位置参数需要放在关键字参数的前面(positional argument follows keyword argument)\n",
    ">> 2.关键字参数放在末尾（均是关键字，不考虑顺序）\n",
    "* >2.默认参数\n",
    ">> 1.非默认参数需要放在默认参数的前面(non-default argument follows default argument)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2c4a1115",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (<ipython-input-11-1deb49c661a6>, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-11-1deb49c661a6>\"\u001b[1;36m, line \u001b[1;32m6\u001b[0m\n\u001b[1;33m    func_text(x=1,2)\u001b[0m\n\u001b[1;37m                  ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "# eg1 错误示范;关键字参数在位置参数前\n",
    "def func_text(x,y):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    \n",
    "func_text(x=1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7d0c482f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# eg2\n",
    "def func_test(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)\n",
    "    \n",
    "func_test(2,1,z=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9896877e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "5\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# eg3 默认参数与位置参数同时使用时\n",
    "def func_test(x,z,y=3):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)\n",
    "    \n",
    "func_test(2,3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91915e9b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a0e3aba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
